unit uWyborKlienta;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, GridsEh, DBGridEh, ExtCtrls, TWApplePanel,
  AdvCombo;

type
  TFWyborKlienta = class(TForm)
    twAppleBox1: TtwAppleBox;
    DBGridEh1: TDBGridEh;
    lbKlienci: TListBox;
    qKlient: TADOQuery;
    dsKlient: TDataSource;
    edId: TEdit;
    edNazwa: TEdit;
    Bevel1: TBevel;
    Button1: TButton;
    Button2: TButton;
    btAddOne: TButton;
    btDelOne: TButton;
    btDelAll: TButton;
    cbSieci: TAdvComboBox;
    qTemp: TADOQuery;
    procedure btAddOneClick(Sender: TObject);
    procedure btDelAllClick(Sender: TObject);
    procedure btDelOneClick(Sender: TObject);
    procedure edChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cbSieciClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Klienci : string;
  end;

var
  FWyborKlienta: TFWyborKlienta;

implementation

{$R *.dfm}

procedure TFWyborKlienta.btAddOneClick(Sender: TObject);
var
  s : string;
begin
  if not qKlient.IsEmpty then begin
    s := trim(qKlient.FieldByName('CUSTNMBR').AsString);
    if lbKlienci.Items.IndexOf(s)<0 then    
      lbKlienci.Items.Add(s);
  end;
end;

procedure TFWyborKlienta.btDelOneClick(Sender: TObject);
begin
  if lbKlienci.ItemIndex>=0 then
    lbKlienci.Items.Delete(lbKlienci.ItemIndex);
end;

procedure TFWyborKlienta.Button1Click(Sender: TObject);
begin
  lbKlienci.Items.Clear;
  Close;
end;

procedure TFWyborKlienta.Button2Click(Sender: TObject);
var
  i : integer;
begin
  Klienci := '';
  for i  := 0 to lbKlienci.Items.Count - 1 do
    Klienci := Klienci + lbKlienci.Items[i]+',';
  Klienci := copy(Klienci, 1, length(Klienci)-1);  
  Close;
end;

procedure TFWyborKlienta.cbSieciClick(Sender: TObject);
var
  Klient : string;
  i : integer;
begin
  with qTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT Klient FROM MBA.dbo.TW_KlientSiec WHERE Siec='''+cbSieci.Text+'''');
    Open;
    while not EOF do
    begin
      klient := Trim(qTemp.FieldByName('Klient').AsString);
      for i := 0 to lbKlienci.Items.Count - 1 do
        if lbKlienci.Items[i] = klient then
          klient := '';
      if klient <> '' then
        lbKlienci.Items.Add(Klient);
      Next;
    end;
    Close;
  end;
end;

procedure TFWyborKlienta.edChange(Sender: TObject);
var
  s : string;
begin
  qKlient.Filtered := False;
  s := '';
  if trim(edID.Text)<> '' then begin
    s := 'CUSTNMBR like '''+trim(edID.Text)+'%'' ';
    if trim(edNazwa.Text)<>'' then
      s := s + ' and CUSTNAME like ''%'+trim(edNazwa.Text)+'%'' ';
  end else if trim(edNazwa.Text)<>'' then begin
    s := 'CUSTNAME like ''%'+trim(edNazwa.Text)+'%'' ';
  end;
  if s<>'' then begin
    qKlient.Filter := s;
    qKlient.Filtered := True;
  end;
end;

procedure TFWyborKlienta.FormShow(Sender: TObject);
begin
  qKlient.Open;
end;

procedure TFWyborKlienta.btDelAllClick(Sender: TObject);
begin
  lbKlienci.Items.Clear;
end;

end.
